from sqlalchemy.orm import Session
from . import models, schemas

def get_mc_cash_record(db: Session, mc_cash_record_id: int):
    return db.query(models.McCashRecord).filter(models.McCashRecord.id == mc_cash_record_id).first()

def get_mc_cash_records(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McCashRecord).offset(skip).limit(limit).all()

def create_mc_cash_record(db: Session, mc_cash_record: schemas.McCashRecordCreate):
    db_mc_cash_record = models.McCashRecord(**mc_cash_record.dict())
    db.add(db_mc_cash_record)
    db.commit()
    db.refresh(db_mc_cash_record)
    return db_mc_cash_record

def get_mc_chats_record(db: Session, mc_chats_record_id: int):
    return db.query(models.McChatsRecord).filter(models.McChatsRecord.id == mc_chats_record_id).first()

def get_mc_chats_records(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McChatsRecord).offset(skip).limit(limit).all()

def create_mc_chats_record(db: Session, mc_chats_record: schemas.McChatsRecordCreate):
    db_mc_chats_record = models.McChatsRecord(**mc_chats_record.dict())
    db.add(db_mc_chats_record)
    db.commit()
    db.refresh(db_mc_chats_record)
    return db_mc_chats_record

def update_mc_chats_record(db: Session, mc_chats_record_id: int, mc_chats_record: schemas.McChatsRecordUpdate):
    db_mc_chats_record = db.query(models.McChatsRecord).filter(models.McChatsRecord.id == mc_chats_record_id).first()
    if db_mc_chats_record:
        for var, value in vars(mc_chats_record).items():
            setattr(db_mc_chats_record, var, value) if value else None
        db.add(db_mc_chats_record)
        db.commit()
        db.refresh(db_mc_chats_record)
    return db_mc_chats_record

def delete_mc_chats_record(db: Session, mc_chats_record_id: int):
    db_mc_chats_record = db.query(models.McChatsRecord).filter(models.McChatsRecord.id == mc_chats_record_id).first()
    if db_mc_chats_record:
        db.delete(db_mc_chats_record)
        db.commit()
    return db_mc_chats_record
def update_mc_cash_record(db: Session, mc_cash_record_id: int, mc_cash_record: schemas.McCashRecordUpdate):
    db_mc_cash_record = db.query(models.McCashRecord).filter(models.McCashRecord.id == mc_cash_record_id).first()
    if db_mc_cash_record:
        for var, value in vars(mc_cash_record).items():
            setattr(db_mc_cash_record, var, value) if value else None
        db.add(db_mc_cash_record)
        db.commit()
        db.refresh(db_mc_cash_record)
    return db_mc_cash_record

def delete_mc_cash_record(db: Session, mc_cash_record_id: int):
    db_mc_cash_record = db.query(models.McCashRecord).filter(models.McCashRecord.id == mc_cash_record_id).first()
    if db_mc_cash_record:
        db.delete(db_mc_cash_record)
        db.commit()
    return db_mc_cash_record
# McMappingFans CRUD
def get_mc_mapping_fans(db: Session, fanid: int):
    return db.query(models.McMappingFans).filter(models.McMappingFans.fanid == fanid).first()

def get_mc_mapping_fanss(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMappingFans).offset(skip).limit(limit).all()

def create_mc_mapping_fans(db: Session, mc_mapping_fans: schemas.McMappingFansCreate):
    db_mc_mapping_fans = models.McMappingFans(**mc_mapping_fans.dict())
    db.add(db_mc_mapping_fans)
    db.commit()
    db.refresh(db_mc_mapping_fans)
    return db_mc_mapping_fans

def update_mc_mapping_fans(db: Session, fanid: int, mc_mapping_fans: schemas.McMappingFansUpdate):
    db_mc_mapping_fans = db.query(models.McMappingFans).filter(models.McMappingFans.fanid == fanid).first()
    if db_mc_mapping_fans:
        for var, value in vars(mc_mapping_fans).items():
            setattr(db_mc_mapping_fans, var, value) if value else None
        db.add(db_mc_mapping_fans)
        db.commit()
        db.refresh(db_mc_mapping_fans)
    return db_mc_mapping_fans

def delete_mc_mapping_fans(db: Session, fanid: int):
    db_mc_mapping_fans = db.query(models.McMappingFans).filter(models.McMappingFans.fanid == fanid).first()
    if db_mc_mapping_fans:
        db.delete(db_mc_mapping_fans)
        db.commit()
    return db_mc_mapping_fans

# McMassRecord CRUD
def get_mc_mass_record(db: Session, id: int):
    return db.query(models.McMassRecord).filter(models.McMassRecord.id == id).first()

def get_mc_mass_records(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMassRecord).offset(skip).limit(limit).all()

def create_mc_mass_record(db: Session, mc_mass_record: schemas.McMassRecordCreate):
    db_mc_mass_record = models.McMassRecord(**mc_mass_record.dict())
    db.add(db_mc_mass_record)
    db.commit()
    db.refresh(db_mc_mass_record)
    return db_mc_mass_record

def update_mc_mass_record(db: Session, id: int, mc_mass_record: schemas.McMassRecordUpdate):
    db_mc_mass_record = db.query(models.McMassRecord).filter(models.McMassRecord.id == id).first()
    if db_mc_mass_record:
        for var, value in vars(mc_mass_record).items():
            setattr(db_mc_mass_record, var, value) if value else None
        db.add(db_mc_mass_record)
        db.commit()
        db.refresh(db_mc_mass_record)
    return db_mc_mass_record

def delete_mc_mass_record(db: Session, id: int):
    db_mc_mass_record = db.query(models.McMassRecord).filter(models.McMassRecord.id == id).first()
    if db_mc_mass_record:
        db.delete(db_mc_mass_record)
        db.commit()
    return db_mc_mass_record

# McCreditsRecharge CRUD
def get_mc_credits_recharge(db: Session, mc_credits_recharge_id: int):
    return db.query(models.McCreditsRecharge).filter(models.McCreditsRecharge.id == mc_credits_recharge_id).first()

def get_mc_credits_recharges(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McCreditsRecharge).offset(skip).limit(limit).all()

def create_mc_credits_recharge(db: Session, mc_credits_recharge: schemas.McCreditsRechargeCreate):
    db_mc_credits_recharge = models.McCreditsRecharge(**mc_credits_recharge.dict())
    db.add(db_mc_credits_recharge)
    db.commit()
    db.refresh(db_mc_credits_recharge)
    return db_mc_credits_recharge

def update_mc_credits_recharge(db: Session, mc_credits_recharge_id: int, mc_credits_recharge: schemas.McCreditsRechargeUpdate):
    db_mc_credits_recharge = db.query(models.McCreditsRecharge).filter(models.McCreditsRecharge.id == mc_credits_recharge_id).first()
    if db_mc_credits_recharge:
        for var, value in vars(mc_credits_recharge).items():
            setattr(db_mc_credits_recharge, var, value) if value else None
        db.add(db_mc_credits_recharge)
        db.commit()
        db.refresh(db_mc_credits_recharge)
    return db_mc_credits_recharge

def delete_mc_credits_recharge(db: Session, mc_credits_recharge_id: int):
    db_mc_credits_recharge = db.query(models.McCreditsRecharge).filter(models.McCreditsRecharge.id == mc_credits_recharge_id).first()
    if db_mc_credits_recharge:
        db.delete(db_mc_credits_recharge)
        db.commit()
    return db_mc_credits_recharge

# McCreditsRecord CRUD
def get_mc_credits_record(db: Session, mc_credits_record_id: int):
    return db.query(models.McCreditsRecord).filter(models.McCreditsRecord.id == mc_credits_record_id).first()

def get_mc_credits_records(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McCreditsRecord).offset(skip).limit(limit).all()

def create_mc_credits_record(db: Session, mc_credits_record: schemas.McCreditsRecordCreate):
    db_mc_credits_record = models.McCreditsRecord(**mc_credits_record.dict())
    db.add(db_mc_credits_record)
    db.commit()
    db.refresh(db_mc_credits_record)
    return db_mc_credits_record

def update_mc_credits_record(db: Session, mc_credits_record_id: int, mc_credits_record: schemas.McCreditsRecordUpdate):
    db_mc_credits_record = db.query(models.McCreditsRecord).filter(models.McCreditsRecord.id == mc_credits_record_id).first()
    if db_mc_credits_record:
        for var, value in vars(mc_credits_record).items():
            setattr(db_mc_credits_record, var, value) if value else None
        db.add(db_mc_credits_record)
        db.commit()
        db.refresh(db_mc_credits_record)
    return db_mc_credits_record

def delete_mc_credits_record(db: Session, mc_credits_record_id: int):
    db_mc_credits_record = db.query(models.McCreditsRecord).filter(models.McCreditsRecord.id == mc_credits_record_id).first()
    if db_mc_credits_record:
        db.delete(db_mc_credits_record)
        db.commit()
    return db_mc_credits_record

# MCFansGroups CRUD
def get_mc_fans_group(db: Session, mc_fans_group_id: int):
    return db.query(models.MCFansGroups).filter(models.MCFansGroups.id == mc_fans_group_id).first()

def get_mc_fans_groups(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MCFansGroups).offset(skip).limit(limit).all()

def create_mc_fans_group(db: Session, mc_fans_group: schemas.MCFansGroupsCreate):
    db_mc_fans_group = models.MCFansGroups(**mc_fans_group.dict())
    db.add(db_mc_fans_group)
    db.commit()
    db.refresh(db_mc_fans_group)
    return db_mc_fans_group

def update_mc_fans_group(db: Session, mc_fans_group_id: int, mc_fans_group: schemas.MCFansGroupsUpdate):
    db_mc_fans_group = db.query(models.MCFansGroups).filter(models.MCFansGroups.id == mc_fans_group_id).first()
    if db_mc_fans_group:
        for var, value in vars(mc_fans_group).items():
            setattr(db_mc_fans_group, var, value) if value else None
        db.add(db_mc_fans_group)
        db.commit()
        db.refresh(db_mc_fans_group)
    return db_mc_fans_group

def delete_mc_fans_group(db: Session, mc_fans_group_id: int):
    db_mc_fans_group = db.query(models.MCFansGroups).filter(models.MCFansGroups.id == mc_fans_group_id).first()
    if db_mc_fans_group:
        db.delete(db_mc_fans_group)
        db.commit()
    return db_mc_fans_group

# MCFansTag CRUD
def get_mc_fans_tag(db: Session, mc_fans_tag_id: int):
    return db.query(models.MCFansTag).filter(models.MCFansTag.id == mc_fans_tag_id).first()

def get_mc_fans_tags(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MCFansTag).offset(skip).limit(limit).all()

def create_mc_fans_tag(db: Session, mc_fans_tag: schemas.MCFansTagCreate):
    db_mc_fans_tag = models.MCFansTag(**mc_fans_tag.dict())
    db.add(db_mc_fans_tag)
    db.commit()
    db.refresh(db_mc_fans_tag)
    return db_mc_fans_tag

def update_mc_fans_tag(db: Session, mc_fans_tag_id: int, mc_fans_tag: schemas.MCFansTagUpdate):
    db_mc_fans_tag = db.query(models.MCFansTag).filter(models.MCFansTag.id == mc_fans_tag_id).first()
    if db_mc_fans_tag:
        for var, value in vars(mc_fans_tag).items():
            setattr(db_mc_fans_tag, var, value) if value else None
        db.add(db_mc_fans_tag)
        db.commit()
        db.refresh(db_mc_fans_tag)
    return db_mc_fans_tag

def delete_mc_fans_tag(db: Session, mc_fans_tag_id: int):
    db_mc_fans_tag = db.query(models.MCFansTag).filter(models.MCFansTag.id == mc_fans_tag_id).first()
    if db_mc_fans_tag:
        db.delete(db_mc_fans_tag)
        db.commit()
    return db_mc_fans_tag

# MCFansTagMapping CRUD
def get_mc_fans_tag_mapping(db: Session, mc_fans_tag_mapping_id: int):
    return db.query(models.MCFansTagMapping).filter(models.MCFansTagMapping.id == mc_fans_tag_mapping_id).first()

def get_mc_fans_tag_mappings(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MCFansTagMapping).offset(skip).limit(limit).all()

def create_mc_fans_tag_mapping(db: Session, mc_fans_tag_mapping: schemas.MCFansTagMappingCreate):
    db_mc_fans_tag_mapping = models.MCFansTagMapping(**mc_fans_tag_mapping.dict())
    db.add(db_mc_fans_tag_mapping)
    db.commit()
    db.refresh(db_mc_fans_tag_mapping)
    return db_mc_fans_tag_mapping

def update_mc_fans_tag_mapping(db: Session, mc_fans_tag_mapping_id: int, mc_fans_tag_mapping: schemas.MCFansTagMappingUpdate):
    db_mc_fans_tag_mapping = db.query(models.MCFansTagMapping).filter(models.MCFansTagMapping.id == mc_fans_tag_mapping_id).first()
    if db_mc_fans_tag_mapping:
        for var, value in vars(mc_fans_tag_mapping).items():
            setattr(db_mc_fans_tag_mapping, var, value) if value else None
        db.add(db_mc_fans_tag_mapping)
        db.commit()
        db.refresh(db_mc_fans_tag_mapping)
    return db_mc_fans_tag_mapping

def delete_mc_fans_tag_mapping(db: Session, mc_fans_tag_mapping_id: int):
    db_mc_fans_tag_mapping = db.query(models.MCFansTagMapping).filter(models.MCFansTagMapping.id == mc_fans_tag_mapping_id).first()
    if db_mc_fans_tag_mapping:
        db.delete(db_mc_fans_tag_mapping)
        db.commit()
    return db_mc_fans_tag_mapping

# MCGroups CRUD
def get_mc_group(db: Session, mc_group_id: int):
    return db.query(models.MCGroups).filter(models.MCGroups.groupid == mc_group_id).first()

def get_mc_groups(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MCGroups).offset(skip).limit(limit).all()

def create_mc_group(db: Session, mc_group: schemas.MCGroupsCreate):
    db_mc_group = models.MCGroups(**mc_group.dict())
    db.add(db_mc_group)
    db.commit()
    db.refresh(db_mc_group)
    return db_mc_group

def update_mc_group(db: Session, mc_group_id: int, mc_group: schemas.MCGroupsUpdate):
    db_mc_group = db.query(models.MCGroups).filter(models.MCGroups.groupid == mc_group_id).first()
    if db_mc_group:
        for var, value in vars(mc_group).items():
            setattr(db_mc_group, var, value) if value else None
        db.add(db_mc_group)
        db.commit()
        db.refresh(db_mc_group)
    return db_mc_group

def delete_mc_group(db: Session, mc_group_id: int):
    db_mc_group = db.query(models.MCGroups).filter(models.MCGroups.groupid == mc_group_id).first()
    if db_mc_group:
        db.delete(db_mc_group)
        db.commit()
    return db_mc_group

# MCHandsel CRUD
def get_mc_handsel(db: Session, mc_handsel_id: int):
    return db.query(models.MCHandsel).filter(models.MCHandsel.id == mc_handsel_id).first()

def get_mc_handsels(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.MCHandsel).offset(skip).limit(limit).all()

def create_mc_handsel(db: Session, mc_handsel: schemas.MCHandselCreate):
    db_mc_handsel = models.MCHandsel(**mc_handsel.dict())
    db.add(db_mc_handsel)
    db.commit()
    db.refresh(db_mc_handsel)
    return db_mc_handsel

def update_mc_handsel(db: Session, mc_handsel_id: int, mc_handsel: schemas.MCHandselUpdate):
    db_mc_handsel = db.query(models.MCHandsel).filter(models.MCHandsel.id == mc_handsel_id).first()
    if db_mc_handsel:
        for var, value in vars(mc_handsel).items():
            setattr(db_mc_handsel, var, value) if value else None
        db.add(db_mc_handsel)
        db.commit()
        db.refresh(db_mc_handsel)
    return db_mc_handsel

def delete_mc_handsel(db: Session, mc_handsel_id: int):
    db_mc_handsel = db.query(models.MCHandsel).filter(models.MCHandsel.id == mc_handsel_id).first()
    if db_mc_handsel:
        db.delete(db_mc_handsel)
        db.commit()
    return db_mc_handsel

# McMembers CRUD
def get_mc_member(db: Session, uid: int):
    return db.query(models.McMembers).filter(models.McMembers.uid == uid).first()

def get_mc_members(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMembers).offset(skip).limit(limit).all()

def create_mc_member(db: Session, mc_member: schemas.McMembersCreate):
    db_mc_member = models.McMembers(**mc_member.dict())
    db.add(db_mc_member)
    db.commit()
    db.refresh(db_mc_member)
    return db_mc_member

def update_mc_member(db: Session, uid: int, mc_member: schemas.McMembersUpdate):
    db_mc_member = db.query(models.McMembers).filter(models.McMembers.uid == uid).first()
    if db_mc_member:
        for var, value in vars(mc_member).items():
            setattr(db_mc_member, var, value) if value else None
        db.add(db_mc_member)
        db.commit()
        db.refresh(db_mc_member)
    return db_mc_member

def delete_mc_member(db: Session, uid: int):
    db_mc_member = db.query(models.McMembers).filter(models.McMembers.uid == uid).first()
    if db_mc_member:
        db.delete(db_mc_member)
        db.commit()
    return db_mc_member

# McMemberAddress CRUD
def get_mc_member_address(db: Session, id: int):
    return db.query(models.McMemberAddress).filter(models.McMemberAddress.id == id).first()

def get_mc_member_addresses(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMemberAddress).offset(skip).limit(limit).all()

def create_mc_member_address(db: Session, mc_member_address: schemas.McMemberAddressCreate):
    db_mc_member_address = models.McMemberAddress(**mc_member_address.dict())
    db.add(db_mc_member_address)
    db.commit()
    db.refresh(db_mc_member_address)
    return db_mc_member_address

def update_mc_member_address(db: Session, id: int, mc_member_address: schemas.McMemberAddressUpdate):
    db_mc_member_address = db.query(models.McMemberAddress).filter(models.McMemberAddress.id == id).first()
    if db_mc_member_address:
        for var, value in vars(mc_member_address).items():
            setattr(db_mc_member_address, var, value) if value else None
        db.add(db_mc_member_address)
        db.commit()
        db.refresh(db_mc_member_address)
    return db_mc_member_address

def delete_mc_member_address(db: Session, id: int):
    db_mc_member_address = db.query(models.McMemberAddress).filter(models.McMemberAddress.id == id).first()
    if db_mc_member_address:
        db.delete(db_mc_member_address)
        db.commit()
    return db_mc_member_address

# McMemberFields CRUD
def get_mc_member_fields(db: Session, id: int):
    return db.query(models.McMemberFields).filter(models.McMemberFields.id == id).first()

def get_mc_member_fieldss(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMemberFields).offset(skip).limit(limit).all()

def create_mc_member_fields(db: Session, mc_member_fields: schemas.McMemberFieldsCreate):
    db_mc_member_fields = models.McMemberFields(**mc_member_fields.dict())
    db.add(db_mc_member_fields)
    db.commit()
    db.refresh(db_mc_member_fields)
    return db_mc_member_fields

def update_mc_member_fields(db: Session, id: int, mc_member_fields: schemas.McMemberFieldsUpdate):
    db_mc_member_fields = db.query(models.McMemberFields).filter(models.McMemberFields.id == id).first()
    if db_mc_member_fields:
        for var, value in vars(mc_member_fields).items():
            setattr(db_mc_member_fields, var, value) if value else None
        db.add(db_mc_member_fields)
        db.commit()
        db.refresh(db_mc_member_fields)
    return db_mc_member_fields

def delete_mc_member_fields(db: Session, id: int):
    db_mc_member_fields = db.query(models.McMemberFields).filter(models.McMemberFields.id == id).first()
    if db_mc_member_fields:
        db.delete(db_mc_member_fields)
        db.commit()
    return db_mc_member_fields

# McMemberProperty CRUD
def get_mc_member_property(db: Session, id: int):
    return db.query(models.McMemberProperty).filter(models.McMemberProperty.id == id).first()

def get_mc_member_properties(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McMemberProperty).offset(skip).limit(limit).all()

def create_mc_member_property(db: Session, mc_member_property: schemas.McMemberPropertyCreate):
    db_mc_member_property = models.McMemberProperty(**mc_member_property.dict())
    db.add(db_mc_member_property)
    db.commit()
    db.refresh(db_mc_member_property)
    return db_mc_member_property

def update_mc_member_property(db: Session, id: int, mc_member_property: schemas.McMemberPropertyUpdate):
    db_mc_member_property = db.query(models.McMemberProperty).filter(models.McMemberProperty.id == id).first()
    if db_mc_member_property:
        for var, value in vars(mc_member_property).items():
            setattr(db_mc_member_property, var, value) if value else None
        db.add(db_mc_member_property)
        db.commit()
        db.refresh(db_mc_member_property)
    return db_mc_member_property

def delete_mc_member_property(db: Session, id: int):
    db_mc_member_property = db.query(models.McMemberProperty).filter(models.McMemberProperty.id == id).first()
    if db_mc_member_property:
        db.delete(db_mc_member_property)
        db.commit()
    return db_mc_member_property

# McOauthFans CRUD
def get_mc_oauth_fans(db: Session, id: int):
    return db.query(models.McOauthFans).filter(models.McOauthFans.id == id).first()

def get_mc_oauth_fanss(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.McOauthFans).offset(skip).limit(limit).all()

def create_mc_oauth_fans(db: Session, mc_oauth_fans: schemas.McOauthFansCreate):
    db_mc_oauth_fans = models.McOauthFans(**mc_oauth_fans.dict())
    db.add(db_mc_oauth_fans)
    db.commit()
    db.refresh(db_mc_oauth_fans)
    return db_mc_oauth_fans

def update_mc_oauth_fans(db: Session, id: int, mc_oauth_fans: schemas.McOauthFansUpdate):
    db_mc_oauth_fans = db.query(models.McOauthFans).filter(models.McOauthFans.id == id).first()
    if db_mc_oauth_fans:
        for var, value in vars(mc_oauth_fans).items():
            setattr(db_mc_oauth_fans, var, value) if value else None
        db.add(db_mc_oauth_fans)
        db.commit()
        db.refresh(db_mc_oauth_fans)
    return db_mc_oauth_fans

def delete_mc_oauth_fans(db: Session, id: int):
    db_mc_oauth_fans = db.query(models.McOauthFans).filter(models.McOauthFans.id == id).first()
    if db_mc_oauth_fans:
        db.delete(db_mc_oauth_fans)
        db.commit()
    return db_mc_oauth_fans
